﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.Data.Entity;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_DonViGiaoThongVanTaiController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        //
        // GET: /GPS_HTCapGPLX/
        #region Index
        public ActionResult Index(ViewModelSDONVIGTVT SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SDONVIGTVT>(o => (SearchModel.dDVGTVT_MA == null || o.DVGTVT_MA.StartsWith(SearchModel.dDVGTVT_MA))
                                        && (SearchModel.dDVGTVT_TEN == null || o.DVGTVT_TEN.StartsWith(SearchModel.dDVGTVT_TEN))
                                        && (SearchModel.dDVGTVT_ACTIVE == null || o.DVGTVT_ACTIVE == SearchModel.dDVGTVT_ACTIVE)
                                        && (o.DVGTVT_MAQL == "40"));
            ViewBag.TotalRow = entities.Count();
            var model = new List<SDONVIGTVT>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<SDONVIGTVT>(currentEntity));
            }
            Int16 icount = 1;
            foreach (var item in model)
            {
                item.STT = icount;
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSDONVIGTVT viewModel)
        {
            ViewBag.lstLoaiDV = new SelectList(_db.Filter<SLOAICSDT>(x => x.LOAICSDT_LOAI == "GTVT"), "LOAICSDT_MA", "LOAICSDT_TEN");
            viewModel.lstDVGTVT = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "SO");
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelSDONVIGTVT());
        }

        [HttpPost]
        public ActionResult Create(CrudModelSDONVIGTVT model)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                SDONVIGTVT _model = new SDONVIGTVT();
                GPS.COMMON.Helpers.CopyObject<SDONVIGTVT>(model, ref _model);
                _model.DVGTVT_MA = GetValue_Indentity(model);
                _db.Insert<SDONVIGTVT>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            ViewBag.lstLoaiDV = new SelectList(_db.Filter<SLOAICSDT>(x => x.LOAICSDT_LOAI == "GTVT"), "LOAICSDT_MA", "LOAICSDT_TEN");
            ViewBag.lstDVGTVT = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "SO"), "DVGTVT_MAQL", "DVGTVT_TEN");
        }

        public string GetValue_Indentity(CrudModelSDONVIGTVT model)
        {
            Int64 valueMax = 0;
            string value = "";
            List<SDONVIGTVT> lst = _db.DbContext.SDONVIGTVTs.Where(x => x.DVGTVT_MAQL == model.DVGTVT_MAQL && x.DVGTVT_LOAI == model.DVGTVT_LOAI).ToList();
            if (lst.Count != 0)
            {
                valueMax = lst.Max(e => Convert.ToInt64(e.DVGTVT_MA));
            }
            value = (valueMax + 1).ToString();
            return value;
        }
        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string id)
        {
            CrudModelSDONVIGTVT _model = new CrudModelSDONVIGTVT();
            SDONVIGTVT model = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSDONVIGTVT>(model, ref _model);
            initialCategoryEditAction(_model);
            return View(_model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSDONVIGTVT model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == model.DVGTVT_MA);
                GPS.COMMON.Helpers.CopyObject<SDONVIGTVT>(model, ref _model);
                _db.Update<SDONVIGTVT>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryEditAction(CrudModelSDONVIGTVT view)
        {
            string selectedvalue = _db.Filter<SLOAICSDT>(x => x.LOAICSDT_MA == view.DVGTVT_LOAI).FirstOrDefault().ToString();
            ViewBag.lstLoaiDV = new SelectList(_db.Filter<SLOAICSDT>(x => x.LOAICSDT_LOAI == "GTVT"), "LOAICSDT_MA", "LOAICSDT_TEN", selectedvalue);

            string selectedvalueDVGTVT = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == view.DVGTVT_MAQL).FirstOrDefault().ToString();
            ViewBag.lstDVGTVT = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "SO"), "DVGTVT_MAQL", "DVGTVT_TEN", selectedvalueDVGTVT);
        }
        #endregion

        #region Detele
        public ActionResult Delete(string id)
        {
            CrudModelSDONVIGTVT _model = new CrudModelSDONVIGTVT();
            SDONVIGTVT model = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == id);
            _db.DeleteItem<SDONVIGTVT>(model);
            return RedirectToAction("Index");
        }
        #endregion

        //
        // Detail
        #region Detail
        public ActionResult Detail(string id)
        {
            CrudModelSDONVIGTVT _model = new CrudModelSDONVIGTVT();
            SDONVIGTVT model = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSDONVIGTVT>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("Detail", _model);
        }

        public void initialCategoryDetailAction(CrudModelSDONVIGTVT view)
        {
            if (view.DVGTVT_ACTIVE == "1")
            {
                view.DVGTVT_ACTIVE = "Hiệu Lực";
            }
            else
            {
                view.DVGTVT_ACTIVE = "Không Hiệu Lực";
            }
            SLOAICSDT csdt = _db.GetOne<SLOAICSDT>(o => o.LOAICSDT_MA == view.DVGTVT_LOAI);
            view.DVGTVT_LOAI = csdt.LOAICSDT_TEN;
           
            string selectedvalueDVGTVT = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == view.DVGTVT_MAQL).Select(x => x.DVGTVT_TEN).FirstOrDefault().ToString();
            view.DVGTVT_LOAI = selectedvalueDVGTVT;
        }
        #endregion
    }
}